/**
 * Copy Right@ chenwt
 */

package com.ryenlii.datatrans.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;


/**
 * @author zhengwen
 */
@Configuration
public class Config {

    @Bean
    public SimpleClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        simpleClientHttpRequestFactory.setConnectTimeout(5000);
        simpleClientHttpRequestFactory.setReadTimeout(5000);
        return simpleClientHttpRequestFactory;
    }

    @Bean
    public RestTemplate restTemplate(SimpleClientHttpRequestFactory simpleClientHttpRequestFactory) {
        return new RestTemplate(simpleClientHttpRequestFactory);
    }

    private static Environment environment;

    @Autowired
    public void setEnvironment(Environment environment) {
        Config.environment = environment;
    }

    public Config() {
    }

    public static String getProperty(String key) {
        return environment.getProperty(key);
    }

    public static Integer getPropertyInt(String key) {
        return Integer.valueOf(Objects.requireNonNull(environment.getProperty(key)));
    }

}
